package dao.impl;

import bean.pojo.Medical;
import commons.dbHelper.DBHelper;
import dao.MedicalDao;

import java.util.ArrayList;
import java.util.List;

/**
 * dao.impl
 * User: 李雨行
 * Date: 2024/12/16 18:26
 * motto:   沉淀哥
 * Description:
 * Version: V1.0
 */
public class MedicalDaoImpl implements MedicalDao {
    private static DBHelper dbHelper=new DBHelper();
    @Override
    public List<Medical> findByPage(Integer appointmentId, Integer userId, String medicalItem, Integer page, Integer limit) {
        List<Object> list =new ArrayList<>();
        StringBuffer sql=new StringBuffer("select appointment_id,user_id,medical_item,appointment_time,remarks " +
                "from medical_appointment where 1=1 ");
        if (appointmentId!=null){
            sql.append(" and appointment_id=? ");
            list.add(appointmentId);
        }
        if (userId!=null){
            sql.append(" and user_id=? ");
            list.add(userId);
        }
        if (medicalItem!=null){
            sql.append(" and medical_item=? ");
            list.add(medicalItem);
        }
        sql.append(" order by appointment_id desc ");
        sql.append(" limit "+(page-1)*limit+" , "+limit);
//        System.out.println("sql:"+sql);
        return dbHelper.getBeanList(Medical.class,sql.toString(),list.toArray());

    }

    @Override
    public boolean add(Medical medical) {
        String sql="insert medical_appointment (user_id,medical_item,appointment_time,remarks) values(?,?,?,?)";
        return dbHelper.update(sql,medical.getUserId(),medical.getMedicalItem(),medical.getAppointmentTime(),medical.getRemarks())>0;
    }

    @Override
    public boolean update(Medical medical) {
        String sql="update medical_appointment set user_id=?,medical_item=?,appointment_time=?,remarks=? where appointment_id=?";
        return dbHelper.update(sql,medical.getUserId(),medical.getMedicalItem(),medical.getAppointmentTime(),medical.getRemarks(),medical.getAppointmentId())>0;
    }

    @Override
    public Integer maxPage(Integer appointmentId, Integer userId, String medicalItem, Integer page, Integer limit) {
        List<Object> list =new ArrayList<>();
        StringBuffer sql=new StringBuffer("select appointment_id,user_id,medical_item,appointment_time,remarks " +
                "from medical_appointment where 1=1 ");
        if (appointmentId!=null){
            sql.append(" and appointment_id=? ");
            list.add(appointmentId);
        }
        if (userId!=null){
            sql.append(" and user_id=? ");
            list.add(userId);
        }
        if (medicalItem!=null){
            sql.append(" and medical_item=? ");
            list.add(medicalItem);
        }
        sql.append(" order by appointment_id desc ");
//        System.out.println("sql:"+sql);
        return dbHelper.getBeanList(Medical.class,sql.toString(),list.toArray()).size();
    }


    @Override
    public boolean deleteById(Integer id) {
        String sql="delete from medical_appointment where appointment_id=?";
        return dbHelper.update(sql,id)>0;
    }
}
